package IO;

import java.sql.Driver;

/**
 * 装饰器设计模式
 * 1.抽象组件:需要装饰的抽象对象(接口或抽象父类)
 * 2.具体组件:需要装饰的对象
 * 3.抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
 * 4,具体装饰类:被装饰的对象
 * @author gaofeng
 * @date 2022年05月08日 20:25
 */
public class DecorateTest {
    public static void main(String[] args) {
        Drink coffee = new Coffee() ;
        Suger suger = new Suger(coffee) ;
//        Milk milk = new Milk(coffee) ;
        Milk milk = new Milk(suger) ;
        System.out.println(suger.cost());
        System.out.println(milk.cost());
    }
}

/**
 * 抽象组件
 */
interface Drink {
    /**
     * 花费
     * @return double
     */
    double cost();

    /**
     * 信息
     * @return double
     */
    String info();
}

/**
 * 具体组件
 */
class Coffee implements Drink {

    private String name = "原味咖啡" ;

    @Override
    public double cost() {
        return 10;
    }

    @Override
    public String info() {
        return name;
    }
}

/**
 * 抽象装饰类
 */
 abstract class Decorate implements Drink {
    private Drink drink ;

     public Decorate(Drink drink ) {
         this.drink = drink ;
     }

     @Override
     public double cost() {
         return this.drink.cost() ;
     }

     @Override
     public String info() {
         return this.drink.info();
     }
 }

class Milk extends Decorate {

    public Milk(Drink drink) {
        super(drink);
    }

    @Override
    public double cost() {
        return super.cost() * 4 ;
    }

    @Override
    public String info() {
        return super.info() + "加入了牛奶";
    }
}

class Suger extends Decorate {

    public Suger(Drink drink) {
        super(drink);
    }

    @Override
    public double cost() {
        return super.cost() * 2 ;
    }

    @Override
    public String info() {
        return super.info() + "加入了蔗糖";
    }
}
